/* sort.c 
 *    Test program to sort a large number of integers.
 *
 *    Intention is to stress virtual memory system.
 *
 *    Ideally, we could read the unsorted array off of the file system,
 *	and store the result back to the file system!
 */

#include "syscall.h"

int A[1024];	/* size of physical memory; with code, we'll run out of space!*/

int
main()
{
    int i, j, tmp;

    /* first initialize the array, in reverse sorted order */
    for (i = 0; i < 1024; i++)		
	{
        A[i] = 1023 - i;
	}
	
    /* then sort! */
    for (i = 0; i < 1023; i++)
	{
        for (j = 0; j < (1023 - i); j++)
		{
			if (A[j] > A[j + 1]) 
			{
				tmp = A[j];
				A[j] = A[j + 1];
				A[j + 1] = tmp;
			}
		}
	}
		   
	for(i = 0; i < 1024; i++)
	{
		if(i != A[i])
		{
			if(i < 100)
			{
				FPrint("bettersort failed on element %d!\n", 34, 1, i);
			}
			else
			{
				FPrint("bettersort failed on element %d%d!\n", 36, 2, i);
			}
			
			Exit(A[i]);
		}
	}
	
	FPrint("bettersort successful, all elements are sorted\n", 48, 0, 0);
	
    Exit(A[0]);		/* and then we're done -- should be 0! */
}
